package com.industrial.premu.service.impl;

import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.DictUtils;
import com.industrial.premu.domain.PreMuDifferentiationPolicy;
import com.industrial.premu.mapper.DifferentiationPolicyAnalysisMapper;
import com.industrial.premu.service.IDifferentiationPolicyAnalysisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DifferentiationPolicyAnalysisServiceImpl implements IDifferentiationPolicyAnalysisService {

    @Autowired
    private DifferentiationPolicyAnalysisMapper differentiationPolicyAnalysisMapper;

    /**
     * <p>
     * 查询差别化政策标签数量分布数量
     * </p>
     *
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> queryPolicyCount(PreMuDifferentiationPolicy params) {
        List<Map<String, Object>> maps = differentiationPolicyAnalysisMapper.queryPolicyCount(params);
        return maps;
    }

    /**
     * 各地市差别化政策适用评价等级比对
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryPolicyLevel(PreMuDifferentiationPolicy params) {
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyLevel(params);
        Map<String, List<PreMuDifferentiationPolicy>> jh = new HashMap<>();
        if(result != null && !result.isEmpty()){
            jh = result.stream().collect(Collectors.groupingBy(PreMuDifferentiationPolicy::getReleaseCity));
        }

        List<String> xAxis = new ArrayList<>();
        List<String> aAxis = new ArrayList<>();
        List<String> bAxis = new ArrayList<>();
        List<String> cAxis = new ArrayList<>();
        List<String> dAxis = new ArrayList<>();

        Map<String,Object> resultMap = new HashMap<>();

        for(Map.Entry<String, List<PreMuDifferentiationPolicy>> temp : jh.entrySet()){
            String cityName = temp.getKey();
            xAxis.add(cityName);
            List<PreMuDifferentiationPolicy> value = temp.getValue();
            for(PreMuDifferentiationPolicy tempPolicy : value){
                if(StringUtils.equals(tempPolicy.getCompanyLevel(),"A")){
                    aAxis.add(tempPolicy.getCount());
                }else if(StringUtils.equals(tempPolicy.getCompanyLevel(),"B")){
                    bAxis.add(tempPolicy.getCount());
                }else if(StringUtils.equals(tempPolicy.getCompanyLevel(),"C")){
                    cAxis.add(tempPolicy.getCount());
                }else if(StringUtils.equals(tempPolicy.getCompanyLevel(),"D")){
                    dAxis.add(tempPolicy.getCount());
                }
            }
            //补充占位
            //1.取出4个数组最大的数据长度
            List<Integer> aList = new ArrayList<>();
            aList.add(aAxis.size());
            aList.add(bAxis.size());
            aList.add(cAxis.size());
            aList.add(dAxis.size());
            Integer argsMaxSize = aList.stream().max(Integer::compareTo).get();
            //只要数组中的长度达不到argsMaxSize，直接补0
            for(int i = aAxis.size(); i < argsMaxSize;i++){
                aAxis.add(String.valueOf(0));
            }
            for(int i = bAxis.size(); i < argsMaxSize;i++){
                bAxis.add(String.valueOf(0));
            }
            for(int i = cAxis.size(); i < argsMaxSize;i++){
                cAxis.add(String.valueOf(0));
            }
            for(int i = dAxis.size(); i < argsMaxSize;i++){
                dAxis.add(String.valueOf(0));
            }
        }
        resultMap.put("X",xAxis);
        resultMap.put("A",aAxis);
        resultMap.put("B",bAxis);
        resultMap.put("C",cAxis);
        resultMap.put("D",dAxis);
        return resultMap;
    }

    /**
     * 各部门差别化政策发布数量分析
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryPolicyDeptCount(PreMuDifferentiationPolicy params) {
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyDeptCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return resultMap;
    }


    @Override
    public AjaxResult queryPolicyTdSysCount(PreMuDifferentiationPolicy params) {

        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyTdSysCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);

    }

    @Override
    public AjaxResult queryPolicyTdGyCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyTdGyCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPolicyPwqsfCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyPwqsfCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }


    @Override
    public AjaxResult queryPolicySjCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicySjCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPolicyQjCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyQjCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPolicyDjCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyDjCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult queryPolicyZfxzjCount(PreMuDifferentiationPolicy params) {
        //查询土地供应
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryPolicyZfxzjCount(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        for(PreMuDifferentiationPolicy temp : result){
            String releaseDept = temp.getReleaseDept();
            String cbhReleaseDept = DictUtils.getDictLabel("cbh_release_dept", releaseDept);
            xAxis.add(cbhReleaseDept);

            String count = temp.getCount();
            yAxis.add(count);
        }
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("xAxis",xAxis);
        resultMap.put("yAxis",yAxis);
        return AjaxResult.success(resultMap);
    }

    /**
     * <p>
     * 差别化政策近5年发布数量趋势分析
     * </p>
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryFiveData(PreMuDifferentiationPolicy params) {
        List<PreMuDifferentiationPolicy> result = differentiationPolicyAnalysisMapper.queryFiveData(params);

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        Integer flagYears= 0;
        for(PreMuDifferentiationPolicy temp : result){
            String years = temp.getYears();
            String count = temp.getCount();

            if(flagYears == 0){
                if(StringUtils.endsWith(years,".0")){
                    years = years.replace(".0","");
                }
                flagYears = Integer.valueOf(years);
            }
            if(!flagYears.equals(Integer.valueOf(years))){
                if(StringUtils.endsWith(years,".0")){
                    years = years.replace(".0","");
                }
                xAxis.add(String.valueOf(flagYears));
                yAxis.add("0");
            }

            flagYears++;
            xAxis.add(years);
            yAxis.add(count);
        }

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("x",xAxis);
        resultMap.put("y",yAxis);
        return resultMap;
    }
}
